package com.example.test_websocket

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.test_websocket.websocket.ClubWebSocketClient
import com.example.test_websocket.websocket.WebSocketUtils
import org.java_websocket.handshake.ServerHandshake
import java.net.URI

class MainActivity : AppCompatActivity() {

    private lateinit var editText: EditText
    private lateinit var webSocketUtils: WebSocketUtils
    companion object{
        private const val TAG = "MainActivity"
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        editText =  findViewById<EditText>(R.id.et)
//        //建立链接
//        myWebSocketClient = ClubWebSocketClient(URI("ws://10.161.9.80:7015/liveRoom/websocket/131313/6334"))
//        myWebSocketClient.connect()

        webSocketUtils =  WebSocketUtils.Builder()
            .setUri("ws://10.161.9.80:7015/liveRoom/websocket/131313/6334")
            .setReceiveMessageListener(object : WebSocketUtils.ReceiveMessageListener {
                override fun onMessage(message: String?) {
                    Log.d(TAG, "onMessage: $message")

                }

                override fun onOpen(shake: ServerHandshake?) {
                    Log.d(TAG, "链接成功")
                }

                override fun onClose(code: Int, reason: String?, remote: Boolean) {
                    Log.d(TAG, "关闭链接")
                }

                override fun onError(ex: Exception?) {
                    Log.d(TAG, "链接失败")

                }
            }).build()

    }

    override fun onDestroy() {
        super.onDestroy()
        webSocketUtils.closeConnect()
    }

    fun sendMesage(view: View) {
        webSocketUtils.sendMsg(editText.text.toString())
    }
}